<?php

// Constructs the first results and calls 
// bc_serach_result_prices to get all of the price.
function bc_search_result($bc_main_item, $search) {

	extract($bc_main_item);
	
	//var_dump($bc_main_item);
	
	if(!isset($ASIN) and bc_isbn_test($search) != 1) {
		bc_no_result_error($search);
	}
	else {
		if(bc_isbn_test($search) == 1) {
			$ASIN = bc_isbn_validator($search);
			$is_isbn = 1;
		}
		elseif( bc_isbn_test($ASIN) == 1 ) { 
			$ASIN = bc_isbn_validator($ASIN);
			$is_isbn = 1;
		}
		else
			$is_isbn = 0;
		
		//echo "My ASIN test: " . bc_isbn_test($search) . " ASIN : " . $ASIN;
		// Get Informaiton
		$bc_amazon_details = bc_amazon_details_gen($ASIN);
		$bc_amazon_images = bc_amazon_image_gen($ASIN);

		// Extract Information
		extract($bc_amazon_details);
		extract($bc_amazon_images);
	
		if(isset($LargeImage))
			$bc_image = $LargeImage;
		elseif(isset($MediumImage))
			$bc_image = $MediumImage;
		elseif(isset($SmallImage))
			$bc_image = $SmallImage;
		else {
			$bc_image = plugins_url('images/no_photo.jpeg', __FILE__);
		}
	
		// Print Image
		echo "<div class='bc_search_image'><img src='" . $bc_image . "' width='200px' /></div>";
		echo "<div id='bc_search_results'>";
		echo "<a href='" . $DetailPageURL . "' target='_blank'>" . $Title . "</a><br />";
		if (isset($Manufacturer))
			echo "By: " . $Manufacturer . "<br />";
	
		// Get all of the price information
		bc_search_result_prices($bc_main_item, $ASIN, $bc_amazon_details, $is_isbn);
	
		echo "</div>";
		echo "<hr style='clear:both' />";
	}
}

// Takes the UPC and Prints out the first result from a search
function bc_search_result_prices($main_item, $ASIN, $bc_amazon_details, $is_isbn) {
	
	// Get Default Options
	$options = get_option('bc_options');
	extract($options);
	extract($main_item); 
	extract($bc_amazon_details);	
	
	//var_dump($main_item);
	
	// Check to see which price is the lowest
	$bc_amazon_price = bc_price_check($main_item);
	if ( $bc_amazon_price == '' )
		$bc_amazon_price = 'Sold Out';
	
	// Amazon Output
	$bc_amazon = "<div class='bc_search_price_box' id='bc_amazon_search_price_box' >
					<div class='bc_search_price_title'>Amazon</div> 
					<div class='bc_search_price'>
						<a href='" . $DetailPageURL . "' target='_blank'>" . $bc_amazon_price . "</a>
					</div>
				</div>";
	
	// // Format Half Information
	// $bc_half_info = bc_half_details_gen($ASIN);
	// 	if ($bc_half_info != False) {
	// 		$bc_half = "<div class='bc_search_price_box'>
	// 						<div class='bc_search_price_title'>Half/eBay.com</div> 
	// 						<div class='bc_search_price'>
	// 							<a href='" . $bc_half_info['url'] . "' target='_blank'>" . $bc_half_info['price'] . "</a>
	// 						</div>
	// 					</div>";
	// 	}
	// 	else
	 		$bc_half = '';

	// Format all Commission Junction Data
	if ($is_isbn == 1) {
		$bc_cj_info = bc_cj_price_gen($ASIN);
	}
	else
		$bc_cj_info = bc_cj_price_gen($Title);
	
	//$bc_cj_info = bc_cj_price_gen($UPC);
	$bc_cj = '';
		
	foreach ($bc_cj_info as $key => $value) {
			$bc_cj .= "<div class='bc_search_price_box'>
							<div class='bc_search_price_title'>" . $key . "</div> 
							<div class='bc_search_price'>" . $value . "</div>
						</div>";
		}
	
	bc_print_price_output($bc_amazon, $bc_half, $bc_cj);
}

// Print out the information gathered in bc_serach_result_prices
function bc_print_price_output($bc_amazon, $bc_half, $bc_cj) {
	echo $bc_amazon;
	echo $bc_half;
	echo $bc_cj;
}

// Get the other 6 related items
function bc_search_result_other($bc_amazon_price, $search) {
	
	global $bc_printed_results;
	$bc_printed_results = 0;
	
	// If there are other prices then cycle through them
	if( isset($bc_amazon_price) ) {
		$count = 0;
		foreach ($bc_amazon_price as $item) {
			
			// If there was a previous result display all of the results 
			// including the orignial one $i = 0
			if ( isset( $_GET['title']))  {
				if(isset($item['ASIN'])) {
					// Foreach of the 6 other results get the image, link, title
					// using their ASIN
					bc_print_related_item($item, $search);
				}
			}
			// If there was no previous result display results 2-9
			else {
				if( $count != 0 and isset($item['ASIN'])) {
					// Foreach of the 6 other results get the image, link, title
					// using their ASIN
					bc_print_related_item($item, $search);
				}
			} 
			$count += 1;	
		}
	}
}

function bc_print_related_item( $item, $search ) {
	
	extract($item);
	
	// Get Remaining Data
	$bc_amazon_details = bc_amazon_details_gen($ASIN);
	$bc_amazon_images = bc_amazon_image_gen($ASIN);
	
	// If there is no data then escape and don't print
	if(!isset($bc_amazon_details))
		break;

	// Extract Data
	extract($bc_amazon_details);
	extract($bc_amazon_images);
	
	if(!isset($Title) or $Title == '' )
		return;
	
	if(isset($LargeImage))
		$bc_image = $LargeImage;
	elseif(isset($MediumImage))
		$bc_image = $MediumImage;
	elseif(isset($SmallImage))
		$bc_image = $SmallImage;
	else {
		$bc_image = plugins_url('images/no_photo.jpeg', __FILE__);
	}
	
	// Get the Lowest Price
	$bc_amazon_price = bc_price_check($item);
	
	// Create the Compare Buttom Link
	// if( isset( $_GET['past'] ))
	// 	$bc_compare_link = "?search=" . $ASIN . "&action=Search&past=" . strip_tags($_GET['past']);
	// else
	// 	$bc_compare_link = "?search=" . $ASIN . "&action=Search&past=" . $search;
	
	$bc_compare_link = "?search=" . $ASIN . "&action=Search&title=" . $Title;
	
	echo "
		<div class='bc_related_item'>
			<div class='bc_related_image_box'>
				<a href='" . $DetailPageURL . "' target='_blank'><img src='" . $bc_image . "' alt=' " . $Title . "' /></a>
			</div>
			<div class='bc_related_title'>" . $Title . "</div>
			<div class='bc_related_price'>Starting at: <a href='" . $DetailPageURL . "' target='_blank'>" . $bc_amazon_price . "</div>
			<div class='bc_related_link'><a href='" . $bc_compare_link . "'>Compare Prices</a></div>
		</div>
	";
	global $bc_printed_results;
	$bc_printed_results += 1;
	
	if( $bc_printed_results % 3 == 0 )
		echo "<div class='clear'></div>";
	
}

function bc_price_check($main_item) {
	
	extract($main_item);

	$bc_price_array = array();
	
	// If the values are set assign them to $bc_price_array
	if(isset($LowestCollectablePrice))
			$bc_price_array['collectable'] = $LowestCollectablePrice;
	if(isset($LowestNewPrice))
			$bc_price_array['new'] = $LowestNewPrice;
	if(isset($LowestUsedPrice))
			$bc_price_array['used'] = $LowestUsedPrice;
	
	// Return the lowest of the assigned prices
	return min($bc_price_array);	
}
?>